package com.mikel.monitor.controllers;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.roo.addon.json.RooJson;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.mikel.monitor.domain.ZbNetworkState;
import com.mikel.monitor.services.ZbNetworkStateService;

@RequestMapping("/graficos")
@Controller
public class GraficosController {
	
	int n = 15;
	
	@Autowired
	private ZbNetworkStateService zbNetworkStateService;

    @RequestMapping(method = RequestMethod.POST, value = "{id}")
    public void post(@PathVariable Long id, ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
    }

//    @RequestMapping
//    public String index() {
//        return "graficos/historicos";
//    }
    
        
	@RequestMapping(produces = "text/html")
	public ModelAndView index() {
		
//		List<ZbNetworkState> tempList = getNLastRegisters("Temperatura",5);
//		List<ZbNetworkState> lumList = getNLastRegisters("Luminosidad",5);
//		List<ZbNetworkState> intList = getNLastRegisters("Interruptor magnético",5);
//		List<ZbNetworkState> consList = getNLastRegisters("Consumo eléctrico",290);
//		for (ZbNetworkState zbNetworkState : consList) {
//			zbNetworkState.remove();
//		}

	    ModelAndView mav = new ModelAndView();
	    mav.setViewName("graficos/historicos");
//	    mav.addObject("temp_0", tempList.get(0));
//	    mav.addObject("temp_1", tempList.get(1));
//	    mav.addObject("temp_2", tempList.get(2));
//	    mav.addObject("temp_3", tempList.get(3));
//	    mav.addObject("temp_4", tempList.get(4));
//	    mav.addObject("tempList", tempList);
//	    mav.addObject("lumList", lumList);
//	    mav.addObject("intList", intList);
//	    mav.addObject("consList", consList);
//	    mav.addObject("numero", 3);
	    return mav;
	}
	public ZbNetworkState getLastRegister(String nombreParametro) {
//		ZbNetworkState zbNetworkState = ZbNetworkState.findZbNetworkStatesByNombreParametroEquals(nombreParametro).getSingleResult();
		List<ZbNetworkState> zbNetworkStateList = zbNetworkStateService
				.findAllZbNetworkStates();
//		zbNetworkStateList
		ZbNetworkState aux = null;
		for (ZbNetworkState zbNetworkState : zbNetworkStateList) {
			if (zbNetworkState.getNombreParametro().equals(nombreParametro)) {
				aux = zbNetworkState;
			}
		}
		
		return aux;
	}
	
//	@RequestMapping(value="/test1", method=RequestMethod.GET, produces= MediaType.APPLICATION_JSON_VALUE)
	public List<ZbNetworkState> getNLastRegisters(String nombreParametro, int numeroDeRegistros) {
		System.out.print("-");

		List<ZbNetworkState> zbNetworkStateList = zbNetworkStateService
				.findAllZbNetworkStates();
		List<ZbNetworkState> result = new ArrayList<ZbNetworkState>();
		for (int j = zbNetworkStateList.size() - 1; j >= 0; j--) {
			ZbNetworkState zbNetworkState = zbNetworkStateList.get(j);
			if (zbNetworkState.getNombreParametro().equals(nombreParametro)) {
				result.add(zbNetworkState);
			}
			if(result.size()==numeroDeRegistros) break;
		}		
		return result;
	}

    @RequestMapping(value="/temp", method=RequestMethod.GET)
    public @ResponseBody List<ZbNetworkState> getTemp(){
        return getNLastRegisters("Temperatura",n);
    }
    
    @RequestMapping(value="/cons", method=RequestMethod.GET)
    public @ResponseBody List<ZbNetworkState> getCons(){
    	return getNLastRegisters("Consumo eléctrico",n);
    }
    
    @RequestMapping(value="/lum", method=RequestMethod.GET)
    public @ResponseBody List<ZbNetworkState> getLum(){
    	return getNLastRegisters("Luminosidad",n);
    }
    
    @RequestMapping(value="/evt", method=RequestMethod.GET)
    public @ResponseBody List<ZbNetworkState> getEvt(){
    	return getNLastRegisters("Interruptor magnético",10);
    }
    
	public boolean eraseRegister(int id) {
		boolean done = false;
		ZbNetworkState z = zbNetworkStateService.findZbNetworkState((long) id);
		if (z != null) {
			z.remove();
			done = true;
		}
		return done;
	}
    
}
